﻿#include "cxifilterproxy.h"

CxiFilterProxy::CxiFilterProxy(QObject* parent)
	: QSortFilterProxyModel(parent)
{
}

bool CxiFilterProxy::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
{
	if (m_conditions.isEmpty()) return true;

	auto* model = sourceModel();
	if (!model) return true;

	for (auto it = m_conditions.constBegin(); it != m_conditions.constEnd(); ++it)
	{
		int column = it.key();
		const QString& condition = it.value();
		QModelIndex index = model->index(source_row, column, source_parent);
		QVariant data = model->data(index);
		QString strData = data.toString();

		if (!strData.contains(condition, Qt::CaseInsensitive)) return false;
	}
	return true;
}

void CxiFilterProxy::addFilter(int column, const QString& condition)
{
	if (condition.trimmed().isEmpty()) return;
	m_conditions[column] = condition.trimmed();
	invalidateFilter();
}

void CxiFilterProxy::removeFilter(int column)
{
	if (m_conditions.remove(column) > 0) invalidateFilter();
}

void CxiFilterProxy::clearFilter()
{
	m_conditions.clear();
	invalidateFilter();
}